<template>
  <div class="ex-property-cascader" v-if="!optionsModel.showDataSource">
    <el-form-item label="选项签名">
      <el-input v-model="optionsModel.props!.label" />
    </el-form-item>
    <el-form-item label="选项值">
      <el-input v-model="optionsModel.props!.value" />
    </el-form-item>
    <el-form-item label="子节点属性名称">
      <el-input v-model="optionsModel.props!.children" />
    </el-form-item>
    <el-form-item label="级联选项设置">
      <el-button @click="visible = true" type="primary" icon="ele-Edit" round>
        选项设置
      </el-button>
    </el-form-item>
    <el-dialog v-model="visible" title="级联选项设置" width="600">
      <el-alert show-icon :closable="false" title="删除选项时，因将value作为id使用，在编辑时请确保value值的唯一性" type="info" />
      <el-scrollbar height="400px">
        <el-tree :data="optionsModel.options" @node-click="handleNodeClick" :key="cascaderKey" draggable
          :props="options">
          <template #default="{ node, data }">
            <div class="ex-property-col">
              <div class="flex flex-column-center">
                <el-input class="ex-property-col_put ex-mgl-10 mt10" v-model="data[options.label]">
                  <template #prepend>选项名</template>
                </el-input>
                <el-input class="ex-property-col_put ex-mgl-10 mt10" v-model="data[options.value]"><template
                    #prepend>选项值</template></el-input>
                <el-input class="ex-property-col_put ex-mgl-10 mt10" v-model="data[options.url]"><template
                    #prepend>跳转链接</template></el-input>
              </div>
              <div class="flex flex-column-center">
                <span>选择图片</span>
                <div class="upLoadPicBox ml10">
                  <div class="item addItem" style="width:80px;height: 80px;" v-if="data[options.imgUrl] == ''">
                    <el-icon><ele-Plus /></el-icon>
                  </div>
                  <div class="item" style="width:80px;height: 80px;" v-else>
                    <img :src="data[options.imgUrl]" style="width:80px;height: 80px;" />
                    <div class="image-preview-action">
                      <el-icon><ele-Delete  @click="data[options.imgUrl] = ''" /></el-icon>
                    </div>
                  </div>
                </div>
              </div>


              <el-button @click.stop="handleAddChild(data)" class="ex-mgl-10" icon="ele-Plus" type="primary" plain
                circle />
              <el-button @click.stop="handleDel(node, data)" class="ex-mgl-10" icon="ele-Minus" type="danger" plain
                circle />
            </div>
          </template>
        </el-tree>
      </el-scrollbar>
      <template #footer>
        <span>
          <el-button @click="handleAdd" size="default" type="primary">
            添加选项
          </el-button>
          <!-- <el-button @click="openImport" size="default" type="primary">
            导入选项
          </el-button> -->
          <el-button @click="visible = false" size="default">取消</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog v-model="isImport" width="600" title="组件事件编辑" destroy-on-close :close-on-click-modal="false">
      <ex-code-editor v-model="code" :height="500" lang="json" />
      <template #footer>
        <span>
          <el-button @click="confirm" size="default" type="primary">
            确认
          </el-button>
          <el-button @click="isImport = false" size="default">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from "vue";
import { onMessageWarning, getRandomNumber } from "/@/components/fromDesigner/utils";
import { desPropertyProps } from "./property";
import type Node from "element-plus/es/components/tree/src/model/node";

type TreeData = { [key: string]: any };

const props = defineProps(desPropertyProps);

const visible = ref(false);
const isImport = ref(false);
const cascaderKey = ref(props.settingData.id);
const code = ref("");
const options = computed(() => {
  let { label, value, children } = props.optionsModel.props;
  return {
    label: label ? label : "label",
    value: value ? value : "value",
    children: children ? children : "children",
    url: 'url', //跳转链接
    imgUrl:'imgUrl',//图片链接
  };
});
const settingOptions = computed({
  get: () => props.optionsModel.options,
  set: (val) => val
});

watch(
  () => props.optionsModel?.props?.children,
  () => {
    cascaderKey.value = cascaderKey.value + getRandomNumber();
  }
);

const handleNodeClick = () => {
  console.log(settingOptions.value);
};

const getOptions = (value: string | number) => {
  let item: TreeData = {};
  item[options.value.label] = "new option";
  item[options.value.value] = value;
  item[options.value.children] = [];
  item[options.value.url] = "";
  item[options.value.imgUrl] = "";
  return item;
};

const handleAddChild = (data: TreeData) => {
  if (!data[options.value.children]) data[options.value.children] = [];
  let children = data[options.value.children];
  let value = data[options.value.value];
  let len = children.length + 1;
  let text = value + "-" + len;
  children.push(getOptions(text));
};

const handleAdd = () => {
  let len = settingOptions.value.length + 1;
  settingOptions.value.push(getOptions(len));
};

const handleDel = (node: Node, data: TreeData) => {
  const parent = node.parent;
  let parentChildren = parent.data[options.value.children] as TreeData[];
  const children = parentChildren || parent.data;
  const index = children.findIndex((d) => d.value === data.value);
  if (data.children?.length > 0) {
    onMessageWarning("该节点还存在子节点，无法删除");
    return;
  }
  children.splice(index, 1);
};

const openImport = () => {
  const jsonString = JSON.stringify(settingOptions.value, null, 4);
  code.value = jsonString;
  isImport.value = true;
};

const confirm = () => {
  let parseArr = JSON.parse(code.value);
  settingOptions.value = parseArr;
  isImport.value = false;
};
</script>
<style lang="scss" scoped>
.upLoadPicBox {
  display: flex;
  justify-content: flex-start;

  .item {
    width: 80px;
    height: 80px;
    position: relative;

    img {
      width: 80px;
      height: 80px;
    }

    .image-preview-action {
      position: absolute;
      width: 100%;
      height: 100%;
      left: 0;
      top: 0;
      cursor: default;
      text-align: center;
      color: #fff;
      opacity: 0;
      font-size: 14px;
      background-color: rgba(0, 0, 0, .5);
      transition: opacity .3s;
      cursor: pointer;
      text-align: center;
      line-height: 80px;

      .el-icon-delete {
        font-size: 14px;
      }
    }

    &:hover {
      .image-preview-action {
        opacity: 1;
      }
    }
  }

  .addItem {
    border: 1px solid #E2E2E2;
    border-radius: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
  }


}
</style>
